/*!
 * jquery.ajax 重新封装，功能：1、统一提示，2、错误处理
 * 调用方式不变，只需要引入该js文件
 */
(function ($) {
    var token = "";
    try {
        token = $.cookie("token");
    } catch (e) {
        $("body").append("<script src='/plugins/jQuery/jquery.cookie.js' type='text/javascript'><\/script>");
        token = $.cookie("token");
    }
    //备份jquery的ajax方法
    var _ajax = $.ajax;
    //重写jquery的ajax方法
    $.ajax = function (options) {
        var defaults = {
            load_tip: true,
            err_tip: true,
            suc_tip: true,
            type: "post",
            dataType: "json",
            url: null,
            token: token
        };
        var opt = $.extend(defaults, options);
        if (opt.token == undefined) {
            try {
            } catch (e) {
            }
        }
        //备份opt中error和success方法
        var fn = {
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            },
            success: function (data, textStatus) {
            },
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader('Authorization', "Bearer " + opt.token);
            }
        };
        if (opt.error) {
            fn.error = opt.error;
        }
        if (opt.success) {
            fn.success = opt.success;
        }
        var loadi;
        //扩展增强处理
        var _opt = $.extend(opt, {
            // headers: {'Authorization': "Bearer " + opt.token},
            beforeSend: function (XMLHttpRequest) {
                var isNeedLogin = false;
                try {
                    isNeedLogin = is_need_login;
                }
                catch (e) {
                    isNeedLogin = false;
                }
                if (((opt.token == undefined || opt.token == null || opt.token == "")) && !isNeedLogin) {
                    parent.location.href = "/page/home/login.html";
                    return false;
                }
                if (opt.load_tip) {
                    //loadi = layer.msg('正在加载..', {icon: 6, time: -1});
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                if (opt.err_tip) {
                    layer.alert('网络异常刷新重试', {
                        title: "系统提示",
                        icon: 2,
                        skin: 'layer-ext-moon'
                    });
                }
                //错误方法增强处理
                fn.error(XMLHttpRequest, textStatus, errorThrown);
            },
            success: function (data, textStatus) {
                var t = this;
                if (opt.err_tip) {
                    layer.close(loadi);
                    //json转换对象
                    var json = data;
                    if (json.code) {
                        if (json.code == 1001) {
                            layer.open({
                                content: '您 - 请登录', time: 2, icon: 1, end: function () {
                                    parent.location.href = '/page/home/login.html?v=2017071813';
                                }
                            });
                        } else if (json.code == 1002) {
                            layer.open({
                                content: '您 - 登录失效重新登录', time: 2, icon: 0, end: function () {
                                    $.removeCookie('token', {path: '/'});
                                    parent.location.href = '/page/home/login.html?v=2017071813';
                                }
                            });
                        } else if (json.code == 1003) {
                            layer.open({
                                content: '您 - 未得到相关资源的访问授权', time: 2, icon: 0, end: function () {
                                    $.removeCookie('token', {path: '/'});
                                    parent.location.href = '/page/home/login.html?v=2017071813';
                                }
                            });
                        } else if (json.code == 400) {
                            layer.open({content: json.msg, time: 3000, icon: 0});
                        } else if (json.code == 1004) {
                            layer.open({content: '参数不合法', time: 2, icon: 0});
                        } else if (json.code == 1005) {
                            layer.open({
                                content: '请重新登录',
                                time: 1,
                                icon: 0,
                                end: function () {
                                    location.href = '/page/loan/management/login.html';
                                }
                            });
                        } else if (json.code == 1006) {
                            layer.open({icon: 2, content: json.msg, btn: '朕知道了'});
                        } else if (json.code == 500) {
                            //500 或者 ---
                            layer.open({icon: 2, content: json.msg, btn: '朕知道了'});
                        } else {
                            //成功回调方法增强处理 200
                            fn.success(json, textStatus);
                        }
                    } else {
                        fn.success(json, textStatus);
                    }
                } else {
                    fn.success(data, textStatus);
                }
            }
        });
        _ajax(_opt);
    };
})(jQuery);